import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { Setofkey } from "../../../../entity/setofkey.model";
import { SetofkeyService } from "../../../../service/setofkey.service";
import { Observable } from "rxjs/Observable";
import { Command } from "../../../../shared/toolbar.service";
import { Blade } from "../../../../shared/blade/blade.model";
import { BladeData } from "../../../../shared/blade/blade-data";
import { DialogResult } from "../../../../shared/dialog.service";

//自定义状态表达式数据模型
import { MatrixStateExpr } from "../../../../entity/matrix-state-expr.model"

//矩阵逻辑状态表达式
import { MatrixStateGroup } from "../../../../entity/matrix-state-group.model";
import { MatrixLogicGroup } from "../../../../entity/matrix-logic-group.model";
import { MatrixLogic } from "../../../../entity/matrix_logic.model";
import { MatrixLogicService } from "../../../../service/matrix-logic.service";
import { MatrixLogicEditComponent } from "../matrix-logic-edit/matrix-logic-edit.component";

import { MatrixStateService } from "../../../../service/matrix-state.service";

@Component({
    selector: 'app-matrix-logic-list',
    templateUrl: './matrix-logic-list.component.html',
    styleUrls: ['./matrix-logic-list.component.css']
})
export class MatrixLogicListComponent extends BladeContainerBase<MatrixStateExpr> {

    public matrixLogicsExprs: MatrixStateExpr[] = [];

    constructor(
        private matrixLogicService: MatrixLogicService,
        private matrixStateService: MatrixStateService
    ) { super() }

    ngOnInit(): void {
        this.commands = [
            new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
        ];
        super.ngOnInit();
    }

    initData(): void {
        Promise.all([
            // 根据逻辑组id获取状态表达式
            this.matrixLogicService.getMatrixLogicsByLogicGroupid((this.parentEntity as MatrixLogicGroup).logicGroupId),
            // 根据状态组id获取状态无表达式
            this.matrixStateService.getMatrixStatesByStateGroupid((this.parentEntity as MatrixLogicGroup).stateGroupId)
        ])
            .then(res => {
                this.matrixLogicsExprs = [];
                let matrixStatesExpr=[];
                let matrixLogicState =[]
                if(res[0]){
                    matrixStatesExpr = res[0];
                }
                if(res[0]){
                    matrixLogicState = res[1];
                }
            //   console.log(res)
                matrixLogicState.forEach(state => {
                    let matrixLogicExpr = matrixStatesExpr.find(o => o.stateId === state.stateId);
                    
                    let expression = matrixLogicExpr ? matrixLogicExpr.expression :'';
                    if(matrixLogicExpr){
                        this.matrixLogicsExprs.push(new MatrixStateExpr(state.stateGroupId, state.stateId, expression, state.stateName, state.stateColor,matrixLogicExpr.logicId));
                    }
                    // this.matrixLogicsExprs.push(new MatrixStateExpr(state.stateGroupId, state.stateId, expression, state.stateName, state.stateColor,matrixLogicExpr.logicId));
                });
                this.bladeData.isLoading = false;
            })
            .catch(err => {
                this.bladeData.error = err;
            });
    }

    exeEdit(blend: Blade): void {
        if (this.currentEntity != undefined) {
            let data: BladeData = new BladeData();
            data.id = "matrixStateGroupEdit";
            data.title = this.currentEntity.stateName;
            let editMatrixStateGroupBlade = this.bladeService.createBlade(MatrixLogicEditComponent, data, false, this.currentEntity);
            this.currentBlade.bladeService.showBlade(editMatrixStateGroupBlade, this.currentBlade);
        }
    }
    canExeEdit(blend?: Blade): boolean {
        return this.currentEntity != null;
    }

    onSeletedChange(matrixLogicsExprs: MatrixStateExpr) {
        if (this.currentBlade.childrenCanDeactivate()) {
            this.currentEntity = matrixLogicsExprs;
        }
    }

}



// WEBPACK FOOTER //
// ./src/app/module/basic/matrix-config/matrix-logic-list/matrix-logic-list.component.ts